/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Fees;

import Category.CategoryBean;
import Course.CourseBean;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author aamod
 */
public class AddFees extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession sessions=request.getSession();
        try {
            Vector<Object> fees=new Vector<Object>();
            int feeType=Integer.parseInt(request.getParameter("subfeeType"));
            int amount=Integer.parseInt(request.getParameter("amount"));
            String course=request.getParameter("course");
            int courseid=Integer.parseInt(request.getParameter("stream"));
            String stream=request.getParameter("cid");
            if(stream.length()==0 && course.equals("0"))
            {
                stream="0";
            }
            int sem=Integer.parseInt(request.getParameter("subYear"));
            int category=Integer.parseInt(request.getParameter("category"));
            String session=request.getParameter("session");
            Vector<Object> list=Course.Query.viewAll();
            Vector<Object> list1=Category.Query.viewAll();
            if(course.equals("0") && stream.equals("0") && sem==0)
            {
               if(category!=0)
               {
               Iterator it=list.iterator();
               while(it.hasNext())
               {
                   CourseBean cb=(CourseBean)it.next();
                   for(int i=1;i<=cb.getCoursePeriod();i++)
                   {
                      FeesBean fb=new FeesBean();
                      fb.setFeeTypeId(feeType);
                      fb.setAmount(amount);
                      fb.setCourseId(cb.getCourseId());
                      fb.setSteam(cb.getStream());
                      fb.setCoursePeriod(i);
                      fb.setCategoryId(category);
                      fb.setSession(session);
                      fb.setFeesUnique(fb.getFeeTypeId()+"/"+fb.getCourseId()+"/"+fb.getCoursePeriod()+"/"+fb.getCategoryId()+"/"+fb.getSession());
                      fees.add(fb);
                   }
               }
               }
               else
               {
               Iterator it=list.iterator();
               while(it.hasNext())
               {
                   CourseBean cb=(CourseBean)it.next();
                   for(int i=1;i<=cb.getCoursePeriod();i++)
                   {   
                   Iterator it1=list1.iterator();
                   while(it1.hasNext())
                   {
                   CategoryBean ctb=(CategoryBean)it1.next();
                      FeesBean fb=new FeesBean();
                      fb.setFeeTypeId(feeType);
                      fb.setAmount(amount);
                      fb.setCourseId(cb.getCourseId());
                      fb.setSteam(cb.getStream());
                      fb.setCoursePeriod(i);
                      fb.setCategoryId(ctb.getCategoryId());
                      fb.setSession(session);
                      fb.setFeesUnique(fb.getFeeTypeId()+"/"+fb.getCourseId()+"/"+fb.getCoursePeriod()+"/"+fb.getCategoryId()+"/"+fb.getSession());
                      fees.add(fb);
                   }
                   }
               }  
               }
            }
            else if(stream.equals("0") && sem==0)
            {
                if(category!=0)
                {
                    Vector<String> str=Course.Query.getStream(course);
                    for(int l=0;l<str.size();l+=2)
                    {
                      for(int i=1;i<=Course.Query.getCoursePeriod(Integer.parseInt((String)str.get(l)));i++)
                   {
                      FeesBean fb=new FeesBean();
                      fb.setFeeTypeId(feeType);
                      fb.setAmount(amount);
                      fb.setCourseId(Integer.parseInt((String)str.get(l)));
                      fb.setSteam((String)str.get(l+1));
                      fb.setCoursePeriod(i);
                      fb.setCategoryId(category);
                      fb.setSession(session);
                      fb.setFeesUnique(fb.getFeeTypeId()+"/"+fb.getCourseId()+"/"+fb.getCoursePeriod()+"/"+fb.getCategoryId()+"/"+fb.getSession());
                      fees.add(fb);  
                   }
                    }
                }
                else
                {
                    Vector<String> str=Course.Query.getStream(course); 
                    for(int l=0;l<str.size();l+=2)
                    {
                      for(int i=1;i<=Course.Query.getCoursePeriod(Integer.parseInt((String)str.get(l)));i++)
                        {
                        Iterator it2=list1.iterator();
                        while(it2.hasNext())
                         {
                       CategoryBean ctb=(CategoryBean)it2.next();
                      FeesBean fb=new FeesBean();
                      fb.setFeeTypeId(feeType);
                      fb.setAmount(amount);
                      fb.setCourseId(Integer.parseInt((String)str.get(l)));
                      fb.setSteam((String)str.get(l+1));
                      fb.setCoursePeriod(i);
                      fb.setCategoryId(ctb.getCategoryId());
                      fb.setSession(session);
                      fb.setFeesUnique(fb.getFeeTypeId()+"/"+fb.getCourseId()+"/"+fb.getCoursePeriod()+"/"+fb.getCategoryId()+"/"+fb.getSession());
                      fees.add(fb);  
                        }
                         }
                    }
                }
            }
            else if(stream.equals("0") && sem!=0)
            {
                if(category!=0)
                {
                    Vector<String> str=Course.Query.getStream(course);
                    for(int l=0;l<str.size();l+=2)
                    {
                      FeesBean fb=new FeesBean();
                      fb.setFeeTypeId(feeType);
                      fb.setAmount(amount);
                      fb.setCourseId(Integer.parseInt((String)str.get(l)));
                      fb.setSteam((String)str.get(l+1));
                      fb.setCoursePeriod(sem);
                      fb.setCategoryId(category);
                      fb.setSession(session);
                      fb.setFeesUnique(fb.getFeeTypeId()+"/"+fb.getCourseId()+"/"+fb.getCoursePeriod()+"/"+fb.getCategoryId()+"/"+fb.getSession());
                      fees.add(fb);  
                    }
                }
                else
                {
                    Vector<String> str=Course.Query.getStream(course); 
                    for(int l=0;l<str.size();l+=2)
                    {
                        Iterator it2=list1.iterator();
                        while(it2.hasNext())
                         {
                       CategoryBean ctb=(CategoryBean)it2.next();
                      FeesBean fb=new FeesBean();
                      fb.setFeeTypeId(feeType);
                      fb.setAmount(amount);
                      fb.setCourseId(Integer.parseInt((String)str.get(l)));
                      fb.setSteam((String)str.get(l+1));
                      fb.setCoursePeriod(sem);
                      fb.setCategoryId(ctb.getCategoryId());
                      fb.setSession(session);
                      fb.setFeesUnique(fb.getFeeTypeId()+"/"+fb.getCourseId()+"/"+fb.getCoursePeriod()+"/"+fb.getCategoryId()+"/"+fb.getSession());
                      fees.add(fb);  
                        }
                    }
                }
            }
            else if(sem==0)
            {
                if(category!=0)
                {
                     for(int i=1;i<=Course.Query.getCoursePeriod(courseid);i++)
                   {
                      FeesBean fb=new FeesBean();
                      fb.setFeeTypeId(feeType);
                      fb.setAmount(amount);
                      fb.setCourseId(courseid);
                      fb.setSteam(stream);
                      fb.setCoursePeriod(i);
                      fb.setCategoryId(category);
                      fb.setSession(session);
                      fb.setFeesUnique(fb.getFeeTypeId()+"/"+fb.getCourseId()+"/"+fb.getCoursePeriod()+"/"+fb.getCategoryId()+"/"+fb.getSession());
                      fees.add(fb);  
                   }
                }
                else
                {
                     for(int i=1;i<=Course.Query.getCoursePeriod(courseid);i++)
                        {
                    Iterator it2=list1.iterator();
                        while(it2.hasNext())
                         {
                      CategoryBean ctb=(CategoryBean)it2.next();
                      FeesBean fb=new FeesBean();
                      fb.setFeeTypeId(feeType);
                      fb.setAmount(amount);
                      fb.setCourseId(courseid);
                      fb.setSteam(stream);
                      fb.setCoursePeriod(i);
                      fb.setCategoryId(ctb.getCategoryId());
                      fb.setSession(session);
                      fb.setFeesUnique(fb.getFeeTypeId()+"/"+fb.getCourseId()+"/"+fb.getCoursePeriod()+"/"+fb.getCategoryId()+"/"+fb.getSession());
                      fees.add(fb);  
                        }
                        }
                }
            }
            else
            {
                if(category!=0)
                {
                      FeesBean fb=new FeesBean();
                      fb.setFeeTypeId(feeType);
                      fb.setAmount(amount);
                      fb.setCourseId(courseid);
                      fb.setSteam(stream);
                      fb.setCoursePeriod(sem);
                      fb.setCategoryId(category);
                      fb.setSession(session);
                      fb.setFeesUnique(fb.getFeeTypeId()+"/"+fb.getCourseId()+"/"+fb.getCoursePeriod()+"/"+fb.getCategoryId()+"/"+fb.getSession());
                      fees.add(fb);  
                }
                else
                {
                    Iterator it2=list1.iterator();
                        while(it2.hasNext())
                         {
                      CategoryBean ctb=(CategoryBean)it2.next();
                      FeesBean fb=new FeesBean();
                      fb.setFeeTypeId(feeType);
                      fb.setAmount(amount);
                      fb.setCourseId(courseid);
                      fb.setSteam(stream);
                      fb.setCoursePeriod(sem);
                      fb.setCategoryId(ctb.getCategoryId());
                      fb.setSession(session);
                      fb.setFeesUnique(fb.getFeeTypeId()+"/"+fb.getCourseId()+"/"+fb.getCoursePeriod()+"/"+fb.getCategoryId()+"/"+fb.getSession());
                      fees.add(fb);  
                         }
                }
            }
//            Iterator ii=fees.iterator();
//            while(ii.hasNext())
//            {
//                FeesBean fb=(FeesBean)ii.next();
//                out.println(fb.getFeesUnique());
//                out.println();
//            }
//            out.println(fees.size());
            int i=Fees.Query.add(fees);
            if(i!=0)
            {
                sessions.setAttribute("status", "Success");
                sessions.setAttribute("msg"," Added Successfully.");
                response.sendRedirect("/RIT/Settings/Database/AddFees.jsp");
            }
            else
            {
                sessions.setAttribute("status", "Fail");
                sessions.setAttribute("msg", "Fill The Form Correctly and try again.");
                response.sendRedirect("/RIT/Settings/Database/AddFees.jsp");
            }
        }
        catch(Exception e)
        {
            out.println(e);
        }
        finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(AddFees.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(AddFees.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
